<?php
Authenticate::requireTechnic();
if($_SERVER['REQUEST_METHOD'] == 'DELETE'){
    $pdo = Db::getPDO();
    $pdo->beginTransaction();
    try {
        parse_str(file_get_contents("php://input"), $formData);
        $product = Product::instance()->getOne($formData['id']);
        Product::instance()->del($formData['id']);

        if($product['type'] == 'colo'){
            ProductColo::instance()->del($formData['id']);
        }
        else if($product['type'] == 'vps'){
            ProductVPS::instance()->del($formData['id']);
        }
        else if($product['type'] == 'hosting'){
            ProductHosting::instance()->del($formData['id']);
        }
        $pdo->commit();
        echo json_encode(array('success'=> true));
    }
    catch(Exception $e) {
        $pdo->rollBack();
        echo json_encode(array('error'=> array('code'=> $e->getCode(), 'message'=> $e->getMessage())));
        throw $e;
    }
}
else if($_SERVER['REQUEST_METHOD'] == 'GET'){
    try {
        $product = Product::instance()->getOne($_GET['id']);
        if($product['type'] == 'colo'){
            $product = array_merge($product, ProductColo::instance()->getOne($_GET['id']));
        }
        else if($product['type'] == 'vps'){
            $product = array_merge($product, ProductVPS::instance()->getOne($_GET['id']));
        }
        else if($product['type'] == 'hosting'){
            $product = array_merge($product, ProductHosting::instance()->getOne($_GET['id']));
        }
        $response = array('data'=> $product);
        echo json_encode($response);
    }
    catch(Exception $e) {
        echo json_encode(array('error'=> array('code'=> $e->getCode(), 'message'=> $e->getMessage())));
        throw $e;
    }
}
else if($_SERVER['REQUEST_METHOD']=='PUT'){
    Db::getPDO()->beginTransaction();
    try {
        parse_str(file_get_contents("php://input"), $formData);
        $set = $formData;
        unset($set['sub']);
        unset($set['id']);
        unset($set['type']);

        Product::instance()->edit($formData['id'], $set);
        if($formData['type']=='colo')
            ProductColo::instance()->edit($formData['id'], $formData['sub']);
        else if($formData['type']=='vps')
            ProductVPS::instance()->edit($formData['id'], $formData['sub']);
        else if($formData['type']=='hosting')
            ProductHosting::instance()->edit($formData['id'], $formData['sub']);

        $product = Product::instance()->getOne($formData['id']);
        /*
        if($formData['type']=='colo')
            ProductColo::instance()->getOne($formData['id']);
        else if($formData['type']=='vps')
            ProductVPS::instance()->getOne($formData['id']);
        else if($formData['type']=='hosting')
            ProductHosting::instance()->getOne($formData['id']);
        */
        Db::getPDO()->commit();
        $response = array('data'=> array(
            'product'=> $product
        ));
    }
    catch(Exception $e){
        Db::getPDO()->rollBack();
        echo json_encode(array('error'=> array(
            'code'=> $e->getCode(),
            'message'=> $e->getMessage()
        )));
        throw $e;
    }
}
else if($_SERVER['REQUEST_METHOD']=='OPTIONS'){
    parse_str(file_get_contents("php://input"), $formData);
    if($formData['operation']=='initAdd'){
        try {
            $ips = IP::instance()->get(array('status'=> 0));
            $response = array('data'=> array('ips'=> $ips));
            echo json_encode($response);
        }
        catch(Exception $e) {
            echo json_encode(array('error'=> array('code'=> $e->getCode(), 'message'=> $e->getMessage())));
            throw $e;
        }
    }
    else if($formData['operation']=='initEdit')
    {
        try {
            $ips = IP::instance()->get(array('status'=> 0));
            $product = Product::instance()->getOne($formData['id']);
                if(is_null($product))
                    throw new Exception('ไม่พบบริการนี้');

            Table::instanceTable('Price')->joinOneTo('prices', 'packageId', 'id', $product, Table::JOIN_MANY);
            if($product['type']=='colo'){
                $product = array_merge($product, ProductColo::instance()->getOne($formData['id']));
            } else if($product['type']=='vps') {
                $product = array_merge($product, ProductVPS::instance()->getOne($formData['id']));
            } else if($product['type']=='hosting') {
                $product = array_merge($product, ProductHosting::instance()->getOne($formData['id']));
            }

            $product['account'] = Account::instance()->getOne($product['accountId'], array('password'=> false));
            $response = array('data'=> array('ips'=> $ips, 'product'=> $product));
            echo json_encode($response);
        }
        catch(Exception $e) {
            echo json_encode(array('error'=> array('code'=> $e->getCode(), 'message'=> $e->getMessage())));
            throw $e;
        }
    }
}